package activemq.xmg.com.hellomqtt.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.common.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import activemq.xmg.com.hellomqtt.R;
import activemq.xmg.com.hellomqtt.service.MQService;
import activemq.xmg.com.hellomqtt.utils.CrcUtil;
import at.favre.lib.bytes.Bytes;

public class SettingActivity extends AppCompatActivity {


    private Spinner typeSpinner, typeSpinner2;
    private EditText sonsorEt, newIdEt, uploadTimeEt;
    private Button subBtn, updateIdBtn, setTimeBtn, sampleBtn, rebootBtn;
    private TextView dataTv;

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_setting);

        mContext = this;
        initView();
    }

    private void initView(){
        typeSpinner = (Spinner) findViewById(R.id.typeSpinner);
        typeSpinner2 = (Spinner) findViewById(R.id.typeSpinner2);
        sonsorEt = (EditText) findViewById(R.id.sonsorEt);
        newIdEt = (EditText) findViewById(R.id.newIdEt);
        uploadTimeEt = (EditText) findViewById(R.id.uploadTimeEt);

        dataTv = (TextView) findViewById(R.id.dataTv);

        subBtn = findViewById(R.id.subBtn);
        updateIdBtn = findViewById(R.id.updateIdBtn);
        setTimeBtn = findViewById(R.id.setTimeBtn);
        sampleBtn = findViewById(R.id.sampleBtn);
        rebootBtn = findViewById(R.id.rebootBtn);

        subBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (subBtn.getText().toString().equals("订阅")){
                    subBtn.setText("停止订阅" );
                    MQService.actionStart(mContext);

                }else {
                    subBtn.setText("订阅");
                    MQService.actionStop(mContext);
                }
            }
        });
        updateIdBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MQService.getIntance() == null){
                    Toast.makeText(mContext, "mq服务没有启动", Toast.LENGTH_SHORT).show();
                }else {
                    setNewId();
//                    MQService.getIntance().sendMessage("apk123-T", "hello apk123".getBytes(), 1, false);
                }
            }
        });
        setTimeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MQService.getIntance() == null){
                    Toast.makeText(mContext, "mq服务没有启动", Toast.LENGTH_SHORT).show();
                }else {
                    setTime();
//                    MQService.getIntance().sendMessage("apk123-T", "hello apk123".getBytes(), 1, false);
                }
            }
        });
        sampleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MQService.getIntance() == null){
                    Toast.makeText(mContext, "mq服务没有启动", Toast.LENGTH_SHORT).show();
                }else {
//                    MQService.getIntance().sendMessage("apk123-T", "hello apk123".getBytes(), 1, false);
                    sampleStart();
                }
            }
        });
        rebootBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MQService.getIntance() == null){
                    Toast.makeText(mContext, "mq服务没有启动", Toast.LENGTH_SHORT).show();
                }else {
                    reboot();
//                    MQService.getIntance().sendMessage("apk123-T", "hello apk123".getBytes(), 1, false);
                }
            }
        });

        typeSpinner = (Spinner) findViewById(R.id.typeSpinner);
        String[] mItems = getResources().getStringArray(R.array.spinnername);
        ArrayAdapter<String> _Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mItems);
        typeSpinner.setAdapter(_Adapter);
        typeSpinner2.setAdapter(_Adapter);

        typeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        typeSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void setNewId(){
        if (TextUtils.isEmpty(sonsorEt.getText().toString())){
            Toast.makeText(mContext, "请输入传感器ID", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(newIdEt.getText().toString())){
            Toast.makeText(mContext, "请输入新传感器ID", Toast.LENGTH_SHORT).show();
            return;
        }
        List<Byte> setIDCmd = new ArrayList<>();
        //类型
        int typeIndex = typeSpinner.getSelectedItemPosition();
        setIDCmd.add(getType( typeIndex));

        //加入设备ID
        setIDCmd.addAll(Bytes.from(Integer.parseInt(sonsorEt.getText().toString())).toList());
        //4 重启
        setIDCmd.add((byte)1);
        //类型
        setIDCmd.add(getType( typeIndex));
        setIDCmd.addAll(Bytes.from(Integer.parseInt(newIdEt.getText().toString())).toList());


        byte[] crc = CrcUtil.calcCrc16(Bytes.from(setIDCmd).array());
        setIDCmd.addAll(Bytes.from(crc).toList());
        System.out.println("setIDCmd===" + Bytes.from(setIDCmd).encodeHex());

        MQService.getIntance().sendMessage("WH-HFS-R", Bytes.from(setIDCmd).array(), 1, false);

    }

    private void setTime(){
        if (TextUtils.isEmpty(sonsorEt.getText().toString())){
            Toast.makeText(mContext, "请输入传感器ID", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(uploadTimeEt.getText().toString())){
            Toast.makeText(mContext, "请输入时间", Toast.LENGTH_SHORT).show();
            return;
        }
        List<Byte> timeCmd = new ArrayList<>();
        //类型
        int typeIndex = typeSpinner.getSelectedItemPosition();
        timeCmd.add(getType( typeIndex));

        //加入设备ID
        timeCmd.addAll(Bytes.from(Integer.parseInt(sonsorEt.getText().toString())).toList());
        //4 重启
        timeCmd.add((byte)2);
        timeCmd.add((byte)0);

        timeCmd.addAll(Bytes.from(Integer.parseInt(uploadTimeEt.getText().toString())).toList());


        byte[] crc = CrcUtil.calcCrc16(Bytes.from(timeCmd).array());
        timeCmd.addAll(Bytes.from(crc).toList());
        System.out.println("timeCmd===" + Bytes.from(timeCmd).encodeHex());

        MQService.getIntance().sendMessage("WH-HFS-R", Bytes.from(timeCmd).array(), 1, false);

    }

    private void sampleStart(){
        if (TextUtils.isEmpty(sonsorEt.getText().toString())){
            Toast.makeText(mContext, "请输入传感器ID", Toast.LENGTH_SHORT).show();
            return;
        }

        List<Byte> startCmd = new ArrayList<>();
        //类型
        int typeIndex = typeSpinner.getSelectedItemPosition();
        startCmd.add(getType( typeIndex));

        //加入设备ID
        startCmd.addAll(Bytes.from(Integer.parseInt(sonsorEt.getText().toString())).toList());
        //4 重启
        startCmd.add((byte)3);
        startCmd.add((byte)0);
        startCmd.add((byte)0);
        startCmd.add((byte)0);
        startCmd.add((byte)0);
        startCmd.add((byte)0);

        byte[] crc = CrcUtil.calcCrc16(Bytes.from(startCmd).array());
        startCmd.addAll(Bytes.from(crc).toList());
        System.out.println("startCmd===" + Bytes.from(startCmd).encodeHex());

        MQService.getIntance().sendMessage("WH-HFS-R", Bytes.from(startCmd).array(), 1, false);

    }

    private void reboot(){
        if (TextUtils.isEmpty(sonsorEt.getText().toString())){
            Toast.makeText(mContext, "请输入传感器ID", Toast.LENGTH_SHORT).show();
            return;
        }
        final List<Byte> rebootCmd = new ArrayList<>();
        //类型
        int typeIndex = typeSpinner.getSelectedItemPosition();
        rebootCmd.add(getType( typeIndex));

        //加入设备ID
        rebootCmd.addAll(Bytes.from(Integer.parseInt(sonsorEt.getText().toString())).toList());
        //4 重启
        rebootCmd.add((byte)4);
        rebootCmd.add((byte)0);
        rebootCmd.add((byte)0);
        rebootCmd.add((byte)0);
        rebootCmd.add((byte)0);
        rebootCmd.add((byte)0);

        byte[] crc = CrcUtil.calcCrc16(Bytes.from(rebootCmd).array());
        rebootCmd.addAll(Bytes.from(crc).toList());
        LogUtil.d("rebootCmd===" + Bytes.from(rebootCmd).encodeHex());
        System.out.println("rebootCmd===" + Bytes.from(rebootCmd).encodeHex());

        new Thread(new Runnable() {
            @Override
            public void run() {
                MQService.getIntance().sendMessage("WH-HFS-R", Bytes.from(rebootCmd).array(), 1, false);
            }
        }).start();


    }

    private byte getType(int typeIndex){
        if (typeIndex == 0){
            return 00;
        }else if (typeIndex == 1){
            return 01;
        }else if (typeIndex == 2){
            return 02;
        }else {
            return 03;
        }
    }




}